package com.lww.feature.test.java8datetime;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Set;

/**
 *  * @see <a href="http://www.infoq.com/cn/news/2013/01/jsr310-java8">优缺点介绍</a>
 * User: yian
 * Date: 2017/4/26
 */
public class test {

    @Test
    public void instant() {
        Instant begin = Instant.now();
        System.out.println(begin);
        System.out.println(begin.plus(5, ChronoUnit.SECONDS));
        System.out.println(begin.minusMillis(50));
        System.out.println(begin.isBefore(Instant.now()));
        System.out.println(begin.toEpochMilli());
    }

    @Test
    public void duration() throws InterruptedException {
        Instant begin = Instant.now();
        Thread.sleep(6000+99);
        Instant end = Instant.now();
        Duration elapsed = Duration.between(begin, end);
        System.out.println(elapsed.getSeconds());
        System.out.println(elapsed.toMinutes());

        System.out.println(elapsed.toMillis());
        System.out.println(elapsed.toNanos());
        System.out.println(elapsed.toHours());
        System.out.println(elapsed.dividedBy(10).toMillis());
        System.out.println(elapsed.dividedBy(10).minus(Duration.ofMillis(10)).toMillis());
        System.out.println(elapsed.dividedBy(10).minus(Duration.ofMillis(10)).isNegative());
        System.out.println(elapsed.isZero());
        System.out.println(elapsed.plusHours(3).toMillis());
    }

    @Test
    public void localDate() {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        System.out.println(LocalDate.of(2016, 1, 31));
        System.out.println(LocalDate.of(2016, Month.JANUARY, 31));

        System.out.println(LocalDate.of(2016, Month.JANUARY, 31).getDayOfWeek().getValue());
        System.out.println(Year.now().atDay(5));
        System.out.println(Year.of(2016).atMonth(Month.APRIL));

        System.out.println(LocalDate.of(2016, 1, 31).plusMonths(1));
    }

    @Test
    public void period() {
        LocalDate fiveDaysLater = LocalDate.now().plusDays(5);
        System.out.println(fiveDaysLater);
        System.out.println(LocalDate.now().until(fiveDaysLater));
        Period period = LocalDate.now().until(fiveDaysLater).plusMonths(2);
        System.out.println(fiveDaysLater.plus(period));
        System.out.println(period.isNegative());
    }


    @Test
    public void temporalAdjusters() {
        System.out.println(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));
        System.out.println(LocalDate.now().with(TemporalAdjusters.lastInMonth(DayOfWeek.WEDNESDAY)));
        System.out.println(LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.WEDNESDAY)));
    }

    @Test
    public void localTime() {
        System.out.println(LocalTime.now().isBefore(LocalTime.of(16, 2, 1)));
        System.out.println(LocalTime.now().plusHours(2).getHour());
    }

    @Test
    public void localDateTime() {
        System.out.println(LocalDateTime.now().plusDays(3).minusHours(5));
        System.out.println(LocalDateTime.of(2016, 1, 30, 10, 20, 30));
        System.out.println(LocalDateTime.now().plusDays(3).minusHours(5).isAfter(LocalDateTime.of(2018, 1, 30, 10, 20, 30)));

    }

    @Test
    public void zonedDateTime() {
        Set<String> zones = ZoneId.getAvailableZoneIds();
//        System.out.println(zones);

        System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")));
        System.out.println(ZonedDateTime.now(ZoneId.of("Europe/London")));

        System.out.println(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")).minusHours(3)
                .isBefore(ZonedDateTime.now()));
        System.out.println(ZonedDateTime.now(ZoneId.of("Europe/London")).minusHours(3)
                .isBefore(ZonedDateTime.now()));

    }

    @Test
    public void transfer() {
        System.out.println(LocalDateTime.now().atZone(ZoneId.of("Europe/London")));
        System.out.println(ZonedDateTime.now(ZoneId.of("UTC")).toLocalDate());
        System.out.println(Instant.now().atZone(ZoneId.of("Asia/Shanghai")));
        System.out.println(ZonedDateTime.of(LocalDate.now(), LocalTime.now(), ZoneId.of("UTC")).toInstant());
    }


    /**
     * 年       yy: 16      yyyy: 2016
     * 月       M: 1        MM: 01
     * 日       d: 3        dd: 03
     * 周       e: 3        E:    Web
     * 时       H: 9        HH: 09
     * 钟       mm: 02
     * 秒       ss: 00
     * 纳秒      nnnnnn:000000
     * 时区偏移    x: -04     xx:-0400
     */
    @Test
    public void formatAndParse() {
        // 2016-01-31T15:39:31.481
        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now()));
        System.out.println(DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("Asia/Shanghai"))));
        System.out.println(DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("Asia/Shanghai"))));
// Jan 31, 2016 3:50:04 PM
        DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(LocalDateTime.now());
// Sun 2016-01-31 15:50:04
        DateTimeFormatter.ofPattern("E yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());

        YearMonth yyyymm = YearMonth.parse("201612",
                DateTimeFormatter.ofPattern("yyyyMM"));

        System.out.println(yyyymm);
        YearMonth yearMonth = yyyymm.plusMonths(1);
        LocalDateTime localDateTime = yearMonth.atDay(1).atStartOfDay();
        System.out.println(localDateTime);
        System.out.println(DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(localDateTime));
//        System.out.println(LocalDateTime.parse("201601",
//                DateTimeFormatter.ofPattern("yyyyMM")));
        LocalDate.parse("2016-01-31");

    }

    @Test
    public void offset() {


         LocalDateTime test = LocalDateTime.of(2017, Month.JANUARY, 1, 0, 0, 0);

        LocalDateTime localDateTime = test.plusMonths(1);
        System.out.println(test);
        System.out.println(localDateTime);

    }



}
